Telegram Group & Telegram Channel
🌀 Как работает yield в Python?

Когда вы пишете функцию с yield, она превращается в генератор.
Это не просто функция — это объект, который можно итерировать, и при каждой итерации он "просыпается", возвращает значение и снова засыпает.

Пример:
def squares():
for i in range(3):
yield i * i

for x in squares():
print(x)


Вывод:
0
1
4

Подробнее тут.

Важно: yield не только возвращает значение, но и сохраняет состояние функции.
Это значит, что переменные не сбрасываются, цикл не начинается с начала — выполнение продолжается с того же места.

Зачем это нужно?
🟠Работать с большими объёмами данных
🟠Писать "ленивый" код, который экономит память
🟠Делать асинхронные пайплайны и обработчики

Крч если хочешь писать код, который не захлёбывается от объёмов — генераторы тебе в помощь 😢
Please open Telegram to view this post
VIEW IN TELEGRAM



tg-me.com/pythonist_ru/2484
Create:
Last Update:

🌀 Как работает yield в Python?

Когда вы пишете функцию с yield, она превращается в генератор.
Это не просто функция — это объект, который можно итерировать, и при каждой итерации он "просыпается", возвращает значение и снова засыпает.

Пример:

def squares():
for i in range(3):
yield i * i

for x in squares():
print(x)


Вывод:
0
1
4

Подробнее тут.

Важно: yield не только возвращает значение, но и сохраняет состояние функции.
Это значит, что переменные не сбрасываются, цикл не начинается с начала — выполнение продолжается с того же места.

Зачем это нужно?
🟠Работать с большими объёмами данных
🟠Писать "ленивый" код, который экономит память
🟠Делать асинхронные пайплайны и обработчики

Крч если хочешь писать код, который не захлёбывается от объёмов — генераторы тебе в помощь 😢

BY Pythonist.ru - образование по питону




Share with your friend now:
tg-me.com/pythonist_ru/2484

View MORE
Open in Telegram


Pythonist ru образование по питону Telegram | DID YOU KNOW?

Date: |

How Does Bitcoin Work?

Bitcoin is built on a distributed digital record called a blockchain. As the name implies, blockchain is a linked body of data, made up of units called blocks that contain information about each and every transaction, including date and time, total value, buyer and seller, and a unique identifying code for each exchange. Entries are strung together in chronological order, creating a digital chain of blocks. “Once a block is added to the blockchain, it becomes accessible to anyone who wishes to view it, acting as a public ledger of cryptocurrency transactions,” says Stacey Harris, consultant for Pelicoin, a network of cryptocurrency ATMs. Blockchain is decentralized, which means it’s not controlled by any one organization. “It’s like a Google Doc that anyone can work on,” says Buchi Okoro, CEO and co-founder of African cryptocurrency exchange Quidax. “Nobody owns it, but anyone who has a link can contribute to it. And as different people update it, your copy also gets updated.”

Telegram announces Search Filters

With the help of the Search Filters option, users can now filter search results by type. They can do that by using the new tabs: Media, Links, Files and others. Searches can be done based on the particular time period like by typing in the date or even “Yesterday”. If users type in the name of a person, group, channel or bot, an extra filter will be applied to the searches.

Pythonist ru образование по питону from id


Telegram Pythonist.ru - образование по питону
FROM USA